package isjinhao.jlox.grammar.expression.visitor;

import isjinhao.jlox.grammar.expression.represention.*;

/**
 * @Author ISJINHAO
 * @Date 2021/2/25 19:01
 */
public class ExpressionPrintVisitor implements ExpressionVisitor<String> {

    @Override
    public String visitArithmeticExpr(Arithmetic expr) {
        String left = expr.getLeft().accept(this);
        String right = expr.getRight().accept(this);
        return left + " " + expr.getOperator().getLexeme() + " " + right;
    }

    @Override
    public String visitComparisonExpr(Comparison expr) {
        String left = expr.getLeft().accept(this);
        String right = expr.getRight().accept(this);
        return left + " " + expr.getOperator().getLexeme() + " " + right;
    }

    @Override
    public String visitGroupingExpr(Grouping expr) {
        return "(" + expr.getExpression() + ")";
    }

    @Override
    public String visitUnaryExpr(Unary expr) {
        return expr.getOperator().getLexeme() + expr.getRight().accept(this);
    }

    @Override
    public String visitLiteralExpr(Literal expr) {
        return expr.getValue().toString();
    }

    @Override
    public String visitLogicExpr(Logic logic) {
        return logic.getLeft().toString() + " " + logic.getToken().getLexeme() + " " + logic.getRight().toString();
    }

    @Override
    public String visitVariableExpr(Identifier identifier) {
        return identifier.getName().getLexeme();
    }
}
